﻿//-----------------------------------------------------------------------------
// Copyright (c) BaFang Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace BaFangSpider
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using LumiSoft.Net.Mail;

    /// <summary>
    /// Summary description for ListenUsersManager
    /// </summary>
    public class ListenerManager
    {
        string fileName;
        string mailAccount;

        SerializableDictionary<string, string> listeners;

        public ListenerManager(string fileName, string mailAccount)
        {
            this.mailAccount = mailAccount;
            this.fileName = fileName;
            DataManager dataMan = new DataManager(fileName, typeof(SerializableDictionary<string, string>));
            this.listeners = dataMan.LoadData() as SerializableDictionary<string, string>;
 
            if(this.listeners == null)
            {
                this.listeners = new SerializableDictionary<string, string>();
            }
        }

        public int Count
        {
            get
            {
                return this.listeners.Count;
            }
        }

        public string GetAdditional()
        {
            foreach (string s in this.listeners.Values)
            {
                return s;
            }
            return null;
            //if (this.listeners.ContainsKey(number))
            //{
            //    return this.listeners[number];
            //}
            //return null;
        }

        public void NotifyListens(string title, string content)
        {
            int count = 1;
            ArrayList toList = new ArrayList();
            foreach (string to in this.listeners.Keys)
            {
                if (count < 50)
                {
                    toList.Add(to + "@139.com");
                    count++;
                }
                if (count == 50)
                {
                    try
                    {
                        SendMail.Send(toList, title, content, true, this.mailAccount);
                    }
                    catch (Exception e)
                    {
                        Logger logger = Logger.GetLogger();
                        logger.Write(DateTime.Now.ToString() + ":send mail fail." + e.Message);
                    }
                    count = 1;
                    toList.Clear();
                    toList.Add(to);
                }
            }
            // send the remains.
            SendMail.Send(toList, title, content, true, this.mailAccount);
        }

        public void RemoveListener(string number)
        {
            if (this.listeners.ContainsKey(number))
            {
                this.listeners.Remove(number);
            }
            else
            {
                return;
            }

            Save();
            Logger.GetLogger().Write(DateTime.Now.ToString() + "We removed a listener for" + this.fileName +
                "now its interests list is" + this.listeners.Count);
        }

        public void AddListener(string number, string additional)
        {
            if (!this.listeners.ContainsKey(number))
            {
                this.listeners.Add(number, additional);
            }
            else
            { return; }

            Save();

            Logger logger = Logger.GetLogger();
            logger.Write(DateTime.Now.ToString() + "We added a listener for" + this.fileName +
                "now its interests list is" + this.listeners.Count);
        }

        private void Save()
        {
            DataManager dataMan = new DataManager(fileName, typeof(SerializableDictionary<string, string>));
            dataMan.SaveData(this.listeners);
        }

    }
}